package com.leagueautoattack.util;

import com.leagueautoattack.bean.Config;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinUser;

import java.awt.*;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

import static com.sun.jna.platform.win32.WinUser.INPUT.INPUT_MOUSE;
import static com.sun.jna.platform.win32.WinUser.KEYBDINPUT.KEYEVENTF_KEYUP;
import static com.sun.jna.platform.win32.WinUser.KEYBDINPUT.KEYEVENTF_SCANCODE;

public class Utils {

    public static void KeyboardKeyDown(char key) {

        WinUser.INPUT input = new WinUser.INPUT();
        int mappedkey = User32.INSTANCE.MapVirtualKeyEx((byte) User32.INSTANCE.VkKeyScanExW(key,null), 0,null);
        input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
        input.input.setType("ki");
        input.input.ki = new WinUser.KEYBDINPUT();
        input.input.ki.dwFlags = new WinDef.DWORD(KEYEVENTF_SCANCODE);
        input.input.ki.wScan = new WinDef.WORD(mappedkey);

        User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

    }

    public static void KeyboardKeyUp(char key) {

        WinUser.INPUT input = new WinUser.INPUT();
        int mappedkey = User32.INSTANCE.MapVirtualKeyEx((byte) User32.INSTANCE.VkKeyScanExW(key,null), 0,null);
        input.type = new WinDef.DWORD(WinUser.INPUT.INPUT_KEYBOARD);
        input.input.setType("ki");
        input.input.ki = new WinUser.KEYBDINPUT();
        input.input.ki.dwFlags = new WinDef.DWORD(KEYEVENTF_SCANCODE | KEYEVENTF_KEYUP);
        input.input.ki.wScan = new WinDef.WORD(mappedkey);

        User32.INSTANCE.SendInput(new WinDef.DWORD(1), (WinUser.INPUT[]) input.toArray(1), input.size());

    }

    public static void MouseRightButtonDown() {

        WinUser.INPUT input = new WinUser.INPUT();
        input.type = new WinDef.DWORD(INPUT_MOUSE);
        input.input.setType("mi");
        input.input.mi.dwFlags = new WinDef.DWORD(0x0008);

        User32.INSTANCE.SendInput(new WinDef.DWORD(1), new User32.INPUT[]{input}, input.size());

    }

    public static void MouseRightButtonUp() {

        WinUser.INPUT input = new WinUser.INPUT();
        input.type = new WinDef.DWORD(INPUT_MOUSE);
        input.input.setType("mi");
        input.input.mi.dwFlags = new WinDef.DWORD(0x0010);

        User32.INSTANCE.SendInput(new WinDef.DWORD(1), new User32.INPUT[]{input}, input.size());

    }

    public static Config readConfig()  {
        Properties p = new Properties();
        InputStream in = null;
        try {
            in = new FileInputStream(System.getProperty("user.dir") + "\\config\\config.properties");
            p.load(in);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                in.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        Config config = new Config();
        config.setRectangle(new Rectangle(
                Integer.parseInt(p.getProperty("rectangle.x").trim()),
                Integer.parseInt(p.getProperty("rectangle.y").trim()),
                Integer.parseInt(p.getProperty("rectangle.width").trim()),
                Integer.parseInt(p.getProperty("rectangle.height").trim())
        ));
        config.setPreAttack(Float.parseFloat(p.getProperty("preAttack").trim()));
        config.setPostAttack(Float.parseFloat(p.getProperty("postAttack").trim()));
        config.setAutoAttackKey(p.getProperty("autoAttackKey").trim().charAt(0));

        return config;

    }

}
